//
//  ViewController.m
//  调度组任务OC
//
//  Created by YYY on 2017/10/27.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self simpleGroup];
    // Do any additional setup after loading the view, typically from a nib.
}
/*
 调度组
 1:出组和入组要配对出现。
 2:出组大于入组，直接导致程序崩溃。
 3:入组大于出组，notify永远不会执行，因为组不为空。
 4:没有入组和出组，直接执行notify。
 */
-(void)simpleGroup
{
    //创建调度组
    dispatch_group_t group = dispatch_group_create();
    //创建队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
   
    //入组
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        NSLog(@"Download A %@",[NSThread currentThread]);
        //出组
        dispatch_group_leave(group);
    });
    
    //入组
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        NSLog(@"Download B %@",[NSThread currentThread]);
        //出组
        dispatch_group_leave(group);
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"Finish Leave %@",[NSThread currentThread]);
    });
}
- (void)groupTest
{
    //创建调度组
    dispatch_group_t group = dispatch_group_create();
    
    //创建队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    //调度组监听队列调度任务
    dispatch_group_async(group, queue, ^{
        NSLog(@"Download A %@",[NSThread currentThread]);
    });
    
    //调度组监听队列调度任务
    dispatch_group_async(group, queue, ^{
        NSLog(@"Download B %@",[NSThread currentThread]);
    });
  
    //监听调度组任务
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"Finish Leave %@",[NSThread currentThread]);
    });
    //dispatch_notify(group, dispatch_get_main_queue(), notification_block)
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
